package cn.wolfcode.business.service.impl;

import cn.wolfcode.business.domain.BusServiceItem;
import cn.wolfcode.business.mapper.BusServiceItemMapper;
import cn.wolfcode.business.service.IBusServiceItemService;
import cn.wolfcode.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * 服务项Service业务层处理
 * 
 * @author wolfcode
 * @date 2025-09-19
 */
@Service
public class BusServiceItemServiceImpl implements IBusServiceItemService 
{
    @Autowired
    private BusServiceItemMapper busServiceItemMapper;

    /**
     * 查询服务项
     * 
     * @param id 服务项主键
     * @return 服务项
     */
    @Override
    public BusServiceItem selectBusServiceItemById(Long id)
    {
        return busServiceItemMapper.selectBusServiceItemById(id);
    }

    /**
     * 查询服务项列表
     * 
     * @param busServiceItem 服务项
     * @return 服务项
     */
    @Override
    public List<BusServiceItem> selectBusServiceItemList(BusServiceItem busServiceItem)
    {
        return busServiceItemMapper.selectBusServiceItemList(busServiceItem);
    }

    /**
     * 新增服务项
     * 
     * @param busServiceItem 服务项
     * @return 结果
     */
    @Override
    public int insertBusServiceItem(BusServiceItem busServiceItem)
    {
        // 参数合理化校验
        if (busServiceItem == null) {
            throw new RuntimeException("非法参数");
        }
        if(busServiceItem.getName() == null || busServiceItem.getOriginalPrice() == null
        || busServiceItem.getDiscountPrice() == null || busServiceItem.getCarPackage() == null
        || busServiceItem.getServiceCatalog() == null || busServiceItem.getInfo() == null){
            throw new RuntimeException("非法参数");
        }
        // 价格合理化验证
        // 原价 >= 0 && 折扣 >= 0 && 折扣 <= 原价
        // BigDecimal 的运算不能使用运算符 > >= < <=  + - * / 这些
        // 都需要使用方法
        // xx.compareTo(yy)  调用者 > 参数 返回正数   调用者 = 参数 返回0   调用者 < 参数 返回负数
        if(!(busServiceItem.getOriginalPrice().compareTo(BigDecimal.ZERO) >= 0
        && busServiceItem.getDiscountPrice().compareTo(BigDecimal.ZERO) >= 0
        && busServiceItem.getOriginalPrice().compareTo(busServiceItem.getDiscountPrice()) >= 0)){
            throw new RuntimeException("原价、折扣价都需要大于 0 且原价需要大于折扣价");
        }
        // 判断插入的数据是否为套餐
        if(BusServiceItem.CARPACKAGE_NO.equals(busServiceItem.getCarPackage())){
            // 不是套餐, 初始状态为 -->  4 无需审核
            busServiceItem.setAuditStatus(BusServiceItem.AUDITSTATUS_NO_REQUIRED);
        } else {
            // 是套餐, 初始状态为  -->  0 初始化
            busServiceItem.setAuditStatus(BusServiceItem.AUDITSTATUS_INIT);
        }
        // 提高代码安全性设置，将默认的上下架状态设置为 下架状态
        busServiceItem.setSaleStatus(BusServiceItem.SALESTATUS_OFF);
        busServiceItem.setCreateTime(DateUtils.getNowDate());
        return busServiceItemMapper.insertBusServiceItem(busServiceItem);
    }

    /**
     * 修改服务项
     * 
     * @param busServiceItem 服务项
     * @return 结果
     */
    @Override
    public int updateBusServiceItem(BusServiceItem busServiceItem)
    {
        // 参数合理化校验
        if (busServiceItem == null) {
            throw new RuntimeException("非法参数");
        }
        if(busServiceItem.getName() == null || busServiceItem.getOriginalPrice() == null
                || busServiceItem.getDiscountPrice() == null || busServiceItem.getCarPackage() == null
                || busServiceItem.getServiceCatalog() == null || busServiceItem.getInfo() == null){
            throw new RuntimeException("非法参数");
        }
        // 价格合理化验证
        if(!(busServiceItem.getOriginalPrice().compareTo(BigDecimal.ZERO) >= 0
                && busServiceItem.getDiscountPrice().compareTo(BigDecimal.ZERO) >= 0
                && busServiceItem.getOriginalPrice().compareTo(busServiceItem.getDiscountPrice()) >= 0)){
            throw new RuntimeException("原价、折扣价都需要大于 0 且原价需要大于折扣价");
        }
        // 验证是否套餐禁止修改
        // 查询原始对象
        BusServiceItem oldObj = this.selectBusServiceItemById(busServiceItem.getId());
        if (oldObj == null || oldObj.getCarPackage() == null || oldObj.getAuditStatus() == null) {
            throw new RuntimeException("非法参数");
        }
        // 验证是否套餐是否被修改
        if(!oldObj.getCarPackage().equals(busServiceItem.getCarPackage())){
            throw new RuntimeException("是否套餐禁止修改");
        }
        // 上架状态禁止修改
        if(!oldObj.getSaleStatus().equals(busServiceItem.getSaleStatus())
                || busServiceItem.getSaleStatus().equals(BusServiceItem.SALESTATUS_ON)){
            throw new RuntimeException("修改的数据必须是下架状态且上下架状态禁止修改");
        }
        // 验证审批状态
        if(!oldObj.getAuditStatus().equals(busServiceItem.getAuditStatus())){
            throw new RuntimeException("审批状态禁止修改");
        }
        if(BusServiceItem.CARPACKAGE_YES.equals(busServiceItem.getCarPackage())
            && BusServiceItem.AUDITSTATUS_AUDITING.equals(busServiceItem.getAuditStatus())){
            throw new RuntimeException("审批中的套餐禁止修改");
        }
        if(BusServiceItem.CARPACKAGE_YES.equals(busServiceItem.getCarPackage())
            && (BusServiceItem.AUDITSTATUS_APPROVED.equals(busServiceItem.getAuditStatus())
            || BusServiceItem.AUDITSTATUS_REPLY.equals(busServiceItem.getAuditStatus()))){
            // 若状态是审批通过或审批拒绝，我们需要将其状态修改为初始化。
            busServiceItem.setAuditStatus(BusServiceItem.AUDITSTATUS_INIT);
        }
        return busServiceItemMapper.updateBusServiceItem(busServiceItem);
    }

    /**
     * 批量删除服务项
     * 
     * @param ids 需要删除的服务项主键
     * @return 结果
     */
    @Override
    public int deleteBusServiceItemByIds(Long[] ids)
    {
        return busServiceItemMapper.deleteBusServiceItemByIds(ids);
    }

    /**
     * 删除服务项信息
     * 
     * @param id 服务项主键
     * @return 结果
     */
    @Override
    public int deleteBusServiceItemById(Long id)
    {
        return busServiceItemMapper.deleteBusServiceItemById(id);
    }

    /**
     * 上架功能
     * @param id 服务项ID
     * @return 结果
     */
    @Override
    public int saleOn(Long id) {
        // 参数合理化验证
        if(id == null){
            throw new RuntimeException("非法参数");
        }
        BusServiceItem busServiceItem = selectBusServiceItemById(id);
        if(busServiceItem == null){
            throw new RuntimeException("非法参数");
        }
        // 必须是下架的商品才能点击上架操作
        if(BusServiceItem.SALESTATUS_ON.equals(busServiceItem.getSaleStatus())){
            throw new RuntimeException("该服务项已经上架，请勿重复操作");
        }
        // 状态判定 是套餐且状态不是审批通过都禁止上架
        if(BusServiceItem.CARPACKAGE_YES.equals(busServiceItem.getCarPackage()) // 是套餐
            && !BusServiceItem.AUDITSTATUS_APPROVED.equals(busServiceItem.getAuditStatus())) // 不是审批通过
        {
            throw new RuntimeException("该服务项必须审批通过后才可以上架");
        }
        return busServiceItemMapper.changeSaleStatus(id,BusServiceItem.SALESTATUS_ON);
    }

    @Override
    public int saleOff(Long id) {
        // 参数合理化验证
        if(id == null){
            throw new RuntimeException("非法参数");
        }
        BusServiceItem busServiceItem = selectBusServiceItemById(id);
        if(busServiceItem == null){
            throw new RuntimeException("非法参数");
        }
        // 必须是上架的商品才能点击下架操作
        if(BusServiceItem.SALESTATUS_OFF.equals(busServiceItem.getSaleStatus())){
            throw new RuntimeException("该服务项已经下架，请勿重复操作");
        }
        return busServiceItemMapper.changeSaleStatus(id,BusServiceItem.SALESTATUS_OFF);
    }
}
